#include <iostream>

using std::cout;
using std::endl;

struct Stack{
    int *arr;   // 栈数组
    int m; // 容量
    int *top, *bot;

    Stack(int cap):m(cap), arr(new int[cap]), top(new int[2]{-1}), bot(new int[2]{-1}){
        top[1] = m;
        bot[1] = m;
    }
    ~Stack()
    {
        delete [] arr;
        delete [] top;
        delete [] bot;
    }

    bool leftEmpty() const
    {
        return top[0] == bot[0];
    }

    bool rightEmpty() const
    {
        return top[1] == bot[1];
    }

    bool empty() const
    {
        return leftEmpty() && rightEmpty();
    }

    bool full() const
    {
        return top[0] + 1 == top[1];
    }

    void pushLeft(int val)
    {
        if(!full()){
            arr[++top[0]] = val;
        }
    }

    void pushRight(int val)
    {
        if(!full()){
            arr[--top[1]] = val;
        }
    }

    void popLeft()
    {
        if(!leftEmpty())
            top[0]--;
    }

    void popRight()
    {
        if(!rightEmpty())
            top[1]++;
    }

    int left() const
    {
        if(leftEmpty())
            throw "left stack is empty!";
        return arr[top[0]];
    }

    int right() const
    {
        if(rightEmpty())
            throw "right stack is empty!";
        return arr[top[1]];
    }

};

void test1()
{
    struct Stack stk(10);
    stk.pushLeft(-1);
    stk.pushRight(1);
    stk.pushLeft(-2);
    stk.pushRight(2);
    cout << stk.left() << " " << stk.right() << endl;
    stk.popLeft();
    stk.popRight();
    cout << stk.left() << " " << stk.right() << endl;
}

int main(int args, char *argv[])
{
    test1();
    return 0;
}